package com.ruoyi.web.controller.construction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.RentalPic;
import com.ruoyi.system.service.construction.RentalPicService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/construction/pic")
public class RentalPicController extends BaseController
{
    @Autowired
    private RentalPicService rentalPicService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询租赁服务图片信息列表
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:list')")
    @GetMapping("/list")
    public TableDataInfo list(RentalPic rentalPic)
    {
        startPage();
        List<RentalPic> list = rentalPicService.selectRentalPicList(rentalPic);
        return getDataTable(list);
    }

    /**
     * 导出租赁服务图片信息列表
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:export')")
    @Log(title = "租赁服务图片信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RentalPic rentalPic)
    {
        List<RentalPic> list = rentalPicService.selectRentalPicList(rentalPic);
        ExcelUtil<RentalPic> util = new ExcelUtil<RentalPic>(RentalPic.class);
        return util.exportExcel(list, "pic");
    }

    /**
     * 获取租赁服务图片信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:query')")
    @GetMapping(value = "/{rentalPicId}")
    public AjaxResult getInfo(@PathVariable("rentalPicId") Integer rentalPicId)
    {
        return AjaxResult.success(rentalPicService.selectRentalPicById(rentalPicId));
    }

    /**
     * 新增租赁服务图片信息
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:add')")
    @Log(title = "租赁服务图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalPic rentalPic)
    {
        return toAjax(rentalPicService.insertRentalPic(rentalPic));
    }

    /**
     * 修改租赁服务图片信息
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:edit')")
    @Log(title = "租赁服务图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalPic rentalPic)
    {
        return toAjax(rentalPicService.updateRentalPic(rentalPic));
    }

    /**
     * 删除租赁服务图片信息
     */
//    @PreAuthorize("@ss.hasPermi('construction:pic:remove')")
    @Log(title = "租赁服务图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rentalPicIds}")
    public AjaxResult remove(@PathVariable Integer[] rentalPicIds)
    {
        return toAjax(rentalPicService.deleteRentalPicByIds(rentalPicIds));
    }


//    @Log(title = "租赁服务图片信息")
//    @PostMapping("/uploadRental")
//    public AjaxResult uploadRental(@RequestParam("rentalFile") MultipartFile file) throws IOException
//    {
//        if (!file.isEmpty()){
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getRentalPath(), file);
//            return AjaxResult.success("上传成功",avatar);
//        }
//        return AjaxResult.error("上传图片异常，请联系管理员");
//    }

    @PostMapping("/uploadRental")
    public Map<String,Object> uploadRental(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }
}
